package com.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dao.PaperDao;
import com.dao.QuestionDao;
import com.entity.Paper;
import com.entity.Paper_Question;
import com.entity.Question;
import com.service.PaperService;
import com.service.QuestionService;

@Service
@Transactional
public class PaperServiceImpl implements PaperService {

	@Resource
	PaperDao pd;
	@Resource
	QuestionDao qd;
	@Override
	public Map list(Map map) {
		map.put("rows", pd.list(map));
		map.put("total", pd.getCount(map));
		return map;
	}

	@Override
	public Paper get(Integer id) {
		Paper p =pd.get(id);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("qlist", p.getQlist());
		map.put("param",new Question(1));
		p.setRadiolist(qd.list(map));
		map.put("param",new Question(2));
		p.setMultiplelist(qd.list(map));
		return p;
	}

	@Override
	public int getCount(Map map) {
		return pd.getCount(map);
	}

	@Override
	public boolean add(Paper q) {
		if(pd.add(q)>0)
			return true;
		return false;
	}

	@Override
	public boolean edit(Paper q) {
		if(pd.edit(q)>0)
			return true;
		return false;
	}

	@Override
	public boolean delete(Integer id) {
		if(pd.delete(id)>0)
			return true;
		return false;
	}

	@Override
	public boolean addQuestion(List<Paper_Question> pq) {
		if(pd.addQuestion(pq)>0)
			return true;
		return false;
	}

	@Override
	public boolean add(Paper p, int[] id) {
		if(add(p)){
			List<Paper_Question> list = new ArrayList<Paper_Question>();
			for(int i=0;i<id.length-1;i++){
				list.add(new Paper_Question(p.getId(), id[i]));
			}
			if(addQuestion(list)){
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean edit(Paper p, int[] id) {
		if(edit(p)){
			pd.deleteQuestionByPaper(p.getId());
			List<Paper_Question> list = new ArrayList<Paper_Question>();
			for(int i=0;i<id.length-1;i++){
				list.add(new Paper_Question(p.getId(), id[i]));
			}
			if(addQuestion(list)){
				return true;
			}
		}
		return false;
	}
}
